package top.kaoshanji.p1ch02a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;

/**
 * 代码清单-p1ch02a-48页a：恢复乱码的方法
 * author kaoshanji
 * ^_^
 * create time 2025/2/15 22:22
 */
public class Recover {

    private static final Logger logger = LogManager.getLogger(Recover.class);

    public static void main(String[] args) throws UnsupportedEncodingException {
        logger.info("...代码清单-p1ch02a-48页a：恢复乱码的方法...");

        String str = "ÀÏÂí";

        recover(str);

    }

    public static void recover(String str) throws UnsupportedEncodingException {
        String[] charsets = new String[]{"windows-1252","GB18030","Big5","UTF-8"};

        for (int i = 0; i < charsets.length; i++) {
            for (int j = 0; j < charsets.length; j++) {
                if (i != j) {
                    String s = new String(str.getBytes(charsets[i]), charsets[j]);
                    System.out.println("---- 原来编码(A)假设是: "+charsets[j]+", 被错误解读为了(B): "+charsets[i]);
                    System.out.println(s);
                    System.out.println();
                }
            }

        }


    }



}
